import { Controller, Get, Post, Body, Req, Res } from '@nestjs/common'
import {
  ApiBody,
  ApiOperation,
  ApiRequestTimeoutResponse,
  ApiResponse,
  ApiTags
} from '@nestjs/swagger'
import * as svgCaptcha from 'svg-captcha'
import { AppService } from './app.service'
import { AuthService } from './auth/auth.service'
import { Public } from './auth/guards/public.guard'
import { CreateStaffDto } from './modules/staff/dto/create-staff.dto'
import { LoginStaffDto } from './modules/staff/dto/staff.dto'
import { StaffService } from './modules/staff/staff.service'
import { NoAuth } from './role/decorators/no-auth.decorator'
import { FResponse } from './utils/reponse'
@Public()
@NoAuth()
@Controller()
@ApiTags('app')
export class AppController {
  constructor(
    private readonly appService: AppService,
    private readonly authService: AuthService,
    private readonly staffService: StaffService
  ) {}
  @Post('login')
  @ApiOperation({ summary: '登录', description: '登录接口' })
  @ApiBody({ type: LoginStaffDto, description: '登录数据' })
  async login(@Req() req, @Body() body: LoginStaffDto) {
    // const user = await this.authService.login(body)
    // if (!user) {
    //   return FResponse.fail(null, '用户名或密码错误')
    // }
    // return FResponse.success(user)
    if (
      req.session.code.toLocaleLowerCase() === body.code.toLocaleLowerCase()
    ) {
      const user = await this.authService.login(body)
      if (!user) {
        return FResponse.fail(null, '用户名或密码错误')
      }
      return FResponse.success(user)
    } else {
      return FResponse.fail(null, '验证码错误')
    }
  }
  @Get('logout')
  @ApiOperation({ summary: '登出', description: '登出接口' })
  async logout(@Req() req) {
    req.session.destroy()
    return FResponse.success(null, '登出成功')
  }

  @Get('captcha')
  @ApiOperation({ summary: '验证码', description: '验证码接口' })
  createCaptcha(@Req() req, @Res() res) {
    const captcha = svgCaptcha.create({
      size: 4, //生成几个验证码
      fontSize: 50, //文字大小
      width: 100, //宽度
      height: 34, //高度
      background: '#050D19' //背景颜色
    })
    req.session.code = captcha.text //存储验证码记录到session
    res.type('image/svg+xml')
    res.send(captcha.data)
  }
}
